from utils.db_pool import MySQLConnectionPool
import hashlib

def generate_all_mobiles_for_prefix(prefix, batch_size=10000):
    total_numbers = 100000000  # 10^8
    batch = []

    for i in range(total_numbers):
        mobile = f"{prefix}{i:08d}"
        batch.append(mobile)
        if len(batch) == batch_size:
            write_mobiles_to_mysql(batch)
            batch = []

    # 写入最后一批次（如果有）
    if batch:
        write_mobiles_to_mysql(batch)

def write_mobiles_to_mysql(mobiles):
    db_pool = MySQLConnectionPool()
    conn = db_pool.get_connect()
    try:
        with conn.cursor() as cursor:
            for mobile in mobiles:
                mobile_hashed = hashlib.md5(mobile.encode()).hexdigest()
                cursor.execute("INSERT INTO phone (mobile, mobile_md5) VALUES (%s, %s)", (mobile, mobile_hashed,))
            conn.commit()
    except Exception as e:
        print(f"Error writing to database: {e}")
    finally:
        conn.close()
        

# mobile_prefixes = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
#                     "145", "147", "150", "151", "152", "153", "155", "156", "157", "158", "159",
#                     "166", "170", "171", "172", "173", "175", "176", "177", "178", "180", "181",
#                     "182", "183", "184", "185", "186", "187", "188", "189", "198", "199"]

mobile_prefixes = ["133", "134", "135", "136", "137", "138", "139",
                    "145", "147", "150", "151", "152", "153", "155", "156", "157", "158", "159",
                    "166", "170", "171", "172", "173", "175", "176", "177", "178", "180", "181",
                    "182", "183", "184", "185", "186", "187", "188", "189", "198", "199"]

for prefix in mobile_prefixes:
    generate_all_mobiles_for_prefix(prefix)
